package led.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class BluetoothConn extends Activity {
	// Debugging
	private static final String TAG = "BluetoothConn";
	private static final boolean D = true;

	private static String versionNo = "1.0.0";

	// Key names received from the BluetoothChatService Handler
	public static final String DEVICE_NAME = "device_name";
	public static final String TOAST = "toast";

	public static final String NOTIFY_UI = "NOTIFY_UI";
	public static final String INCOMING_MSG = "INCOMING_MSG";
	public static final String OUTGOING_MSG = "OUTGOING_MSG";
	public static final String ALERT_MSG = "ALERT_MSG";
	public static final String KEY_ECHO_PREF = "KEY_ECHO_PREF";
	public static final String DEVICE_ADDRESS = "device_address";
	public static final String DISCONNECT_DEVICE_ADDRESS = "disconnected_device_address";

	// Intent request codes
	private static final int REQUEST_CONNECT_DEVICE = 1;
	private static final int REQUEST_ENABLE_BT = 2;
	private static final int REQUEST_PREF_SETTING = 3;
	private static final int REQUEST_DISCONNECT_DEVICE = 4;
	private static final int REQUEST_ABOUT_INFO = 5;

	private static final int DIALOG_ABOUT_INFO = 6;
	private static final int FILE_DIALOG_REQUEST = 7;

	private BluetoothAdapter mBluetoothAdapter = null;
	private NotificationReceiver mNotiReceiver;
	private boolean mMonitor = false;

	public static final String TEST_ACTION = "TEST_ACTION";


	//	private RadioGroup leftOffice,rightOffice;
	private Button OfficeButton1,OfficeButton2,OfficeButton3,OfficeButton4,OfficeButton5,OfficeButton6
	,OfficeButton7,OfficeButton8,OfficeButton9,OfficeButton10;
	private MOnClickListener mClickListener;
	private MOnClickListener1 mClickListener1;

	private SharedPreferences mSharedPreferencesForSeekBar;


	private CircularSeekBar seekbar;
	private MSeekBarChangListener listener;
	private TextView mCurrentPP;

	private TextView total_control;
	private Button onWork,afterWork;

	private List<Button> Office,mAllMode;
	private String[] radioButtonKey = {"1","2","3","4","5","6","7","8","9","10"};
	private String mTotalKey = "total_is_on";
	private String mTotalValue = "mTotalValue";
	private HashMap<Button, String> MapRadioMapKey;
	private Map<Button,String> mRadioButtonForAddr;
	private Map<String,Integer> mAllPreferenceForSeekBar;
	private Map<String,Boolean> mTotalIsOnPreference;
	private Map<String,Integer> mTotalValueMap;
	private Button mCurrentRadioButton,mCurrentWeatherButton;
	private boolean istotalClick = true;
	private boolean isOn = true;
	
	
//	private SharedPreferences statusPreference;

	private Button Sunny,Cloudy,Rainy,Night;
	private List<Button> Weather;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.main);

		IntentFilter mFilter01, mFilter02;
		mFilter01 = new IntentFilter(INCOMING_MSG);
		mFilter02 = new IntentFilter(OUTGOING_MSG);
		mNotiReceiver = new NotificationReceiver();
		registerReceiver(mNotiReceiver, mFilter01);
		registerReceiver(mNotiReceiver, mFilter02);
		registerReceiver(mNotiReceiver, new IntentFilter(ALERT_MSG));
		registerReceiver(mNotiReceiver, new IntentFilter(BluetoothConnController.GET_SERIVICE_STATUS_EVENT));
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		
		
		if (mBluetoothAdapter == null) {
			Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
			finish();
			return;
		}
		//		onWork = (Button) findViewById(R.id.on_work);
		
		init();
		readPreference();
		setAllofficeBtnUnClick();
	}

	
	
	private void init(){
		mCurrentRadioButton = new Button(this);
		mCurrentWeatherButton = new Button(this);
		mAllPreferenceForSeekBar = new HashMap<String, Integer>();
		mTotalIsOnPreference = new HashMap<String, Boolean>();
		mTotalValueMap = new HashMap<String, Integer>();
		MapRadioMapKey = new HashMap<Button, String>();
		mRadioButtonForAddr = new HashMap<Button, String>();
		
		mClickListener1 = new MOnClickListener1();
		Office = new ArrayList<Button>();
		Weather = new ArrayList<Button>();
		mClickListener = new MOnClickListener();
		listener = new MSeekBarChangListener();
		
		
		afterWork = (Button) findViewById(R.id.after_work);
		total_control = (TextView) findViewById(R.id.total_control);
		mCurrentPP = (TextView) findViewById(R.id.percent1);
		seekbar = (CircularSeekBar) findViewById(R.id.seekbar1);
		seekbar.setSeekBarChangeListener(listener);
		seekbar.setMaxProgress(254);
		
		
		Sunny = (Button) findViewById(R.id.sunny);
		Cloudy = (Button) findViewById(R.id.cloudy);
		Rainy = (Button) findViewById(R.id.rainy);
		Night = (Button) findViewById(R.id.night);

		Sunny.setOnClickListener(mClickListener1);
		Cloudy.setOnClickListener(mClickListener1);
		Rainy.setOnClickListener(mClickListener1);
		Night.setOnClickListener(mClickListener1);

		Weather.add(Sunny);
		Weather.add(Cloudy);
		Weather.add(Rainy);
		Weather.add(Night);
		
		OfficeButton1 = (Button) findViewById(R.id.office1);
		OfficeButton2 = (Button) findViewById(R.id.office2);
		OfficeButton3 = (Button) findViewById(R.id.office3);
		OfficeButton4 = (Button) findViewById(R.id.office4);
		OfficeButton5 = (Button) findViewById(R.id.office5);
		OfficeButton6 = (Button) findViewById(R.id.office6);
		OfficeButton7 = (Button) findViewById(R.id.office7);
		OfficeButton8 = (Button) findViewById(R.id.office8);
		OfficeButton9 = (Button) findViewById(R.id.office9);
		OfficeButton10 = (Button) findViewById(R.id.office10);


		
		OfficeButton1.setOnClickListener(mClickListener);
		OfficeButton2.setOnClickListener(mClickListener);
		OfficeButton3.setOnClickListener(mClickListener);
		OfficeButton4.setOnClickListener(mClickListener);
		OfficeButton5.setOnClickListener(mClickListener);
		OfficeButton6.setOnClickListener(mClickListener);
		OfficeButton7.setOnClickListener(mClickListener);
		OfficeButton8.setOnClickListener(mClickListener);
		OfficeButton9.setOnClickListener(mClickListener);
		OfficeButton10.setOnClickListener(mClickListener);
//		OfficeButton1.setBackgroundColor(Color.GRAY);
//		OfficeButton2.setBackgroundColor(Color.GRAY);
//		OfficeButton3.setBackgroundColor(Color.GRAY);
//		OfficeButton4.setBackgroundColor(Color.GRAY);
//		OfficeButton5.setBackgroundColor(Color.GRAY);
//		OfficeButton6.setBackgroundColor(Color.GRAY);
//		OfficeButton7.setBackgroundColor(Color.GRAY);
//		OfficeButton8.setBackgroundColor(Color.GRAY);
//		OfficeButton9.setBackgroundColor(Color.GRAY);
//		OfficeButton10.setBackgroundColor(Color.GRAY);

		mCurrentRadioButton = OfficeButton1;

		

		Office.add(OfficeButton1);
		Office.add(OfficeButton2);
		Office.add(OfficeButton3);
		Office.add(OfficeButton4);
		Office.add(OfficeButton5);
		Office.add(OfficeButton6);
		Office.add(OfficeButton7);
		Office.add(OfficeButton8);
		Office.add(OfficeButton9);
		Office.add(OfficeButton10);

		mCurrentWeatherButton = Sunny;

		
		for(int i = 0,j = 130; i < Office.size();i++,j+=2){
			if(j < 16){
				mRadioButtonForAddr.put(Office.get(i), "0"+(Integer.toHexString(j)));
			}else{
				mRadioButtonForAddr.put(Office.get(i), Integer.toHexString(j));
			}
			System.out.println("mRadioButtons.get(i) = "+Office.get(i)+"and addr = "+Integer.toHexString(j));
		}

		for(int i = 0;i<radioButtonKey.length;i++){
			MapRadioMapKey.put(Office.get(i), radioButtonKey[i]);
		}

		readPreference();
		
		
		
		
		
		if(mTotalIsOnPreference.get("off")){
			
		}
		
//		setAllofficeBtnUnClick();
		
		afterWork.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
//				setStatus_off();
				isOn = false;
				afterWork.setTextColor(Color.parseColor("#FF4040"));
				seekbar.setUserProgress(0);
				
				
			}
		});
		total_control.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				System.out.println("-------------------------------------------------");
				if(istotalClick){
					istotalClick = false;
				}else{
					istotalClick = true;
				}
				mTotalIsOnPreference.put(mTotalKey, istotalClick);
				setAllofficeBtnUnClick();
			}
		});
		
	}
	
	
	
	
	
	
	private void setStatus_off(){
		isOn = false;
		istotalClick = false;
		total_control.setClickable(false);
		total_control.setTextColor(Color.BLACK);
		
		setAllOfficeCanClick(false);
		seekbar.setEnabled(false);
		mTotalIsOnPreference.put("off", true);
		SetSeekBarValue(0);
		afterWork.setTextColor(Color.parseColor("#FF4040"));
		Sunny.setTextColor(Color.BLACK);
		Rainy.setTextColor(Color.BLACK);
		Cloudy.setTextColor(Color.BLACK);
		Night.setTextColor(Color.BLACK);
		
	}
	
	
	private void setAllOfficeCanClick(boolean canClick){
			for(int i = 0;i < Office.size();i++){
				Office.get(i).setClickable(canClick);
				Office.get(i).setTextColor(Color.BLACK);
			}
	}
	
	private void setAllofficeBtnUnClick(){

			if(mTotalIsOnPreference.get(mTotalKey)){
				total_control.setTextColor(Color.parseColor("#FF4040"));
				setTotalValue(mTotalValueMap.get(mTotalValue));
				setAllOfficeCanClick(false);
				if(mTotalValueMap.get(mTotalValue) <= 0){
					afterWork.setTextColor(Color.parseColor("#FF4040"));
					isOn = false;
				}else{
					afterWork.setTextColor(Color.BLACK);
					isOn = true;
				}
				
			}else{
				int value = mAllPreferenceForSeekBar.get(MapRadioMapKey.get(mCurrentRadioButton));
				total_control.setTextColor(Color.BLACK);
				setAllOfficeCanClick(true);
				setOtherRadioButtonClickFalse(mCurrentRadioButton);
				SetSeekBarValue(value);
				
				if(value <= 0){
					afterWork.setTextColor(Color.parseColor("#FF4040"));
					isOn = false;
				}else{
					afterWork.setTextColor(Color.BLACK);
					isOn = true;
				}
				
				
				
				
			}
			
			
			
			
	}

	
	private void setTotalValue(int totalValue){
		istotalClick = true;
//		total_control.setTextColor(Color.GREEN);
		total_control.setTextColor(Color.parseColor("#FF4040"));
		afterWork.setTextColor(Color.BLACK);
		Sunny.setTextColor(Color.BLACK);
		Rainy.setTextColor(Color.BLACK);
		Cloudy.setTextColor(Color.BLACK);
		Night.setTextColor(Color.BLACK);
		for(int i =0;i<radioButtonKey.length;i++){
			System.out.println("===========onWork===============");
			mAllPreferenceForSeekBar.put(radioButtonKey[i], totalValue);
		}
		SetSeekBarValue(totalValue);
	}
	

	private void readPreference(){
		mSharedPreferencesForSeekBar = getSharedPreferences("seekbar_value", MODE_PRIVATE);
		for(int i = 0; i < radioButtonKey.length; i++){
			mAllPreferenceForSeekBar.put(radioButtonKey[i], mSharedPreferencesForSeekBar.getInt(radioButtonKey[i],127));
		}
		istotalClick = mSharedPreferencesForSeekBar.getBoolean(mTotalKey, true);
		mTotalIsOnPreference.put(mTotalKey, istotalClick);
		mTotalIsOnPreference.put("off", mSharedPreferencesForSeekBar.getBoolean("off", true));
		mTotalValueMap.put(mTotalValue, mSharedPreferencesForSeekBar.getInt(mTotalValue, 127));
	}


	public void setOtherRadioButtonClickFalse(Button radioButton){
		for(Button RB:Office){
			if(RB != radioButton){
				RB.setTextColor(Color.BLACK);
			}else{
				RB.setTextColor(Color.parseColor("#7CFC00"));
			}
		}
	}
	public void setOtherWeatherButtonClickFalse(Button weatherButton){
		for(Button RB:Weather){
			if(RB != weatherButton){
				RB.setTextColor(Color.BLACK);
				total_control.setTextColor(Color.BLACK);
				total_control.setTextColor(Color.BLACK);
				afterWork.setTextColor(Color.BLACK);
			}else{
				RB.setTextColor(Color.parseColor("#FF4040"));
			}
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		if (!mBluetoothAdapter.isEnabled()) {
			Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
		} else {
			setupChat();
		}
	}

	@Override
	public synchronized void onResume() {
		super.onResume();
		if (D)
			Log.e(TAG, "+ ON RESUME +");

	}

	private void SetSeekBarValue(int value){
			System.out.println("===============value:"+value);
			System.out.println("-------------------------------------------------------currentProgress:"+value);
			seekbar.setUserProgress(value);
	}






	private void setupChat() {
		Log.d(TAG, "setupChat()");

		//		mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
		//		mConversationView = (ListView) findViewById(R.id.in);
		//		mConversationView.setAdapter(mConversationArrayAdapter);
		Intent startIntent = new Intent(BluetoothConn.this, BluetoothConnController.class);
		startService(startIntent);

		Intent i = new Intent(BluetoothConnController.GET_SERIVICE_STATUS_ACTION);
		sendBroadcast(i);
	}

	@Override
	public synchronized void onPause() {
		super.onPause();

		for(int i = 0; i < radioButtonKey.length; i++){
			mSharedPreferencesForSeekBar.edit().putInt(radioButtonKey[i],mAllPreferenceForSeekBar.get(radioButtonKey[i])).commit();
		}
		mSharedPreferencesForSeekBar.edit().putBoolean(mTotalKey, mTotalIsOnPreference.get(mTotalKey)).commit();
		mSharedPreferencesForSeekBar.edit().putInt(mTotalValue, mTotalValueMap.get(mTotalValue)).commit();
		mSharedPreferencesForSeekBar.edit().putBoolean("off", mTotalIsOnPreference.get("off")).commit();
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mNotiReceiver);
	}

	private void ensureDiscoverable() {
		if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
			Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
			discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
			startActivity(discoverableIntent);
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (D)
			Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);
		switch (requestCode) {
		case REQUEST_CONNECT_DEVICE:
			if (resultCode == Activity.RESULT_OK) {
				String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
				Intent i = new Intent(BluetoothConnController.CONNECT_REQUEST_ACTION);
				i.putExtra(DEVICE_ADDRESS, address);
				sendBroadcast(i);
			}
			break;
		case REQUEST_DISCONNECT_DEVICE:
			if (resultCode == Activity.RESULT_OK) {
				String address = data.getExtras().getString(ConnectedDeviceListActivity.EXTRA_CONNECTED_ADDRESS);
				Intent i = new Intent(BluetoothConnController.DISCONNECT_REQUEST_ACTION);
				i.putExtra(DISCONNECT_DEVICE_ADDRESS, address);
				sendBroadcast(i);

			}
			break;
		case REQUEST_ENABLE_BT:
			if (resultCode == Activity.RESULT_OK) {
				setupChat();
			} else {
				Log.d(TAG, "BT not enabled");
				Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
				finish();
			}
			break;

		case REQUEST_PREF_SETTING:
			if (resultCode == Activity.RESULT_OK) {
				//				SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
			}
			break;

		case REQUEST_ABOUT_INFO:
			if (resultCode == Activity.RESULT_OK) {
				Log.e(TAG, "[onActivityResult] Current version is = " + versionNo);
			}
			break;
		case FILE_DIALOG_REQUEST:
			if (resultCode == Activity.RESULT_OK) {
				Log.d(TAG, "receive FILE_DIALOG_REQUEST result: " + resultCode);
				String file = data.getExtras().getString(FileDialog.RESULT_PATH);
				Intent i = new Intent(BluetoothConnController.SEND_MSG_FROM_BT_ACTION);
				i.putExtra("MESSAGE", file.toString());
				i.putExtra("MODE", BluetoothConnController.MSG_MODE_SEND_FILE);
				sendBroadcast(i);
			}

			break;

		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_ABOUT_INFO:
			return new AlertDialog.Builder(BluetoothConn.this).setIcon(R.drawable.ic_menu_help)
			.setTitle(R.string.about_title).setMessage(R.string.about_content)
			.setPositiveButton(R.string.about_dialog_ok, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int button) {

				}
			}).create();

		}
		return null;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.option_menu, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onPrepareOptionsMenu");
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onMenuOpened");
		boolean status = super.onMenuOpened(featureId, menu);
		MenuItem item = (MenuItem) menu.findItem(R.id.monitor);
		item.setTitle(mMonitor ? "Stop Monitir" : "Start Monitor");
		return status;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.scan:
			// Launch the DeviceListActivity to see devices and do scan
			Intent serverIntent = new Intent(this, DeviceListActivity.class);
			startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
			break;
		case R.id.inquiry:
			// Launch the DeviceListActivity to see devices and do scan
			Intent settintIntent = new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
			startActivity(settintIntent);
			break;
		case R.id.discoverable:
			// Ensure this device is discoverable by others
			ensureDiscoverable();
			break;
		case R.id.disconnect:
			Log.i(TAG, "[onOptionsItemSelected] R.id.disconnect");
			Intent disconnectIntent = new Intent(this, ConnectedDeviceListActivity.class);
			startActivityForResult(disconnectIntent, REQUEST_DISCONNECT_DEVICE);
			break;

		case R.id.setting:
			Intent settingIntent = new Intent(this, SettingPreferencActivity.class);
			startActivityForResult(settingIntent, REQUEST_PREF_SETTING);
			break;
		case R.id.monitor:
			if (mMonitor == false) {

				mMonitor = true;
				item.setTitle("Stop Monitor");
			} else {
				item.setTitle("Start Monitor");
				mMonitor = false;
			}
			// Log.d(TAG,"monitor "+ mMonitor);
			Intent i = new Intent(BluetoothConnController.START_MONITOR_ACTION);
			i.putExtra(BluetoothConnController.MONITOR_STATUS, mMonitor);
			sendBroadcast(i);
			//			mTitle = (TextView) findViewById(R.id.title_right_text);
			//			mTitle.setText("monitor: " + mMonitor);
			break;
		case R.id.file_dialog:
			Intent fileDialog = new Intent(this, FileDialog.class);
			startActivityForResult(fileDialog, FILE_DIALOG_REQUEST);
			break;
		case R.id.about:
			// Intent aboutIntent = new Intent(this, AboutInfoActivity.class);
			// startActivityForResult(aboutIntent,REQUEST_PREF_SETTING );
			showDialog(DIALOG_ABOUT_INFO);
			break;
		default:
			return false;
		}
		return true;
	}

	public class NotificationReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(INCOMING_MSG)) {
				System.out.println("###############################"+intent.getStringExtra("STR"));
			} else if (action.equals(OUTGOING_MSG)) {
			} else if (action.equals(ALERT_MSG)) {
				String displayMsg = intent.getExtras().getString("STR");
				//				mConversationArrayAdapter.add(displayMsg);
				if (intent.getExtras().getInt("COUNTER") > 0) {
					AlertDialog.Builder builder = new AlertDialog.Builder(BluetoothConn.this);
					builder.setIcon(android.R.drawable.stat_notify_error).setTitle("Error").setMessage(displayMsg)
					.setPositiveButton("OK", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int button) {
						}
					}).create();
					AlertDialog alert = builder.create();
					alert.show();
				}
			} else if (action.equals(BluetoothConnController.GET_SERIVICE_STATUS_EVENT)) {
			} else {
				Log.e(TAG, "another action: " + action);
			}
		}

	}

	class MOnClickListener implements OnClickListener{
		public void onClick(View v) {
			System.out.println("!!!!!!!!!!!!!!!!!!!!!!!1");
			Button rr = (Button) v;
			int value = mAllPreferenceForSeekBar.get(MapRadioMapKey.get(v));
		
			if(value <= 0){
				afterWork.setTextColor(Color.parseColor("#FF4040"));
				isOn = false;
			}else{
				afterWork.setTextColor(Color.BLACK);
				isOn = true;
			}
			setOtherRadioButtonClickFalse(rr);
			mCurrentRadioButton = rr;
			SetSeekBarValue(value);
		}
	}

	
	private boolean isModeClick = false;


	private void setstatus_on(){
		isOn = true;
		seekbar.setEnabled(true);
		total_control.setClickable(true);
		setAllOfficeCanClick(true);
		mTotalIsOnPreference.put("off", false);
	}
	
	
	
	class MOnClickListener1 implements OnClickListener{
		public void onClick(View v) {
			Button rr = (Button) v;
			seekbar.setEnabled(true);
			istotalClick = false;
			isModeClick = true;
//			isOn = true;
			mTotalIsOnPreference.put(mTotalKey, false);
			setstatus_on();
			setAllofficeBtnUnClick();
//			setAllOfficeCanClick(true);
			mCurrentWeatherButton = rr;
			setOtherWeatherButtonClickFalse(rr);
			if(rr == Sunny){
				seekbar.setUserProgress(128);//80
			}else if(rr == Cloudy){
				seekbar.setUserProgress(177);//B1
			}else if(rr == Night){
				seekbar.setUserProgress(254);
			}else{//Rainy
				seekbar.setUserProgress(215);//D7
			}
			isModeClick = false;
		}
	}




	private Intent msg_inttent = null;
	String vv = null;
	private void send_cmd(String addr, int current_value){
		
		float pi = (float)((float)current_value/254)*100;
		System.out.println("百分比    "+pi);
		if(current_value<16){
			vv = "0"+Integer.toHexString(current_value).toUpperCase();
		}else{
			vv = Integer.toHexString(current_value).toUpperCase();
		}
		
		System.out.println("send common addr = "+addr+",vv = "+vv);
		
		msg_inttent = new Intent(BluetoothConnController.SEND_MSG_FROM_BT_ACTION);
		msg_inttent.putExtra("MESSAGE", addr+" "+vv);
		msg_inttent.putExtra("MODE", BluetoothConnController.MSG_MODE_SEND_STRING);
		sendBroadcast(msg_inttent);
	}


	class MSeekBarChangListener implements CircularSeekBar.OnSeekChangeListener{
		public void onProgressChange(CircularSeekBar view, int newProgress) {
			mCurrentPP.setText(view.getProgressPercent()+"%");

			System.out.println("----------MapRadioMapKey.get(mCurrentRadioButton):"+MapRadioMapKey.get(mCurrentRadioButton));

			if(istotalClick){
				for(int i = 0;i < radioButtonKey.length;i++){
					mAllPreferenceForSeekBar.put(radioButtonKey[i], newProgress);
				}
					mTotalValueMap.put(mTotalValue, newProgress);
				send_cmd("FE",newProgress);
			}else{
				if(isModeClick){
					for(int i = 0;i < radioButtonKey.length;i++){
						mAllPreferenceForSeekBar.put(radioButtonKey[i], newProgress);
					}
//					mTotalValueMap.put(mTotalValue, newProgress);
					send_cmd("FE",newProgress);
				}else{
					mAllPreferenceForSeekBar.put(MapRadioMapKey.get(mCurrentRadioButton), newProgress);
					send_cmd(mRadioButtonForAddr.get(mCurrentRadioButton),newProgress);
				}
			}
			
			if(newProgress > 0){
				
				afterWork.setTextColor(Color.BLACK);
				isOn = true;
			}else{
				afterWork.setTextColor(Color.parseColor("#FF4040"));
				isOn = false;
			}
			System.out.println("add:"+mRadioButtonForAddr.get(mCurrentRadioButton)+","+"Progress:"+newProgress);
		}
	}
}